package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Lpharmacy;
import com.itgaoshu.hospitalproject.bean.Lrecord;
import com.itgaoshu.hospitalproject.service.LDrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("liao")
public class LDrugController {
    @Autowired
    private LDrugService lDrugService;

    //跳转到drug页面
    @RequestMapping("drug")
    public String toDrug(){
        return "liao/drug";
    }

    //查询所有药品的方法
    @RequestMapping("selDrug")
    @ResponseBody
    public Object selDrug(Lpharmacy lpharmacy,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Lpharmacy> lpharmacies = lDrugService.selDrug(lpharmacy);
        System.out.println(lpharmacies);
        PageInfo pageInfo = new PageInfo(lpharmacies);
        HashMap<String,Object> tableDate = new HashMap<>();
        tableDate.put("code",0);
        tableDate.put("msg","");
        tableDate.put("count",pageInfo.getTotal());
        tableDate.put("data",pageInfo.getList());
        return tableDate;
    }

    //根据id查询该药品的现存的数量
    @RequestMapping("selNum")
    @ResponseBody
    public Object selNum(Integer pharmacyId){
        Lpharmacy lpharmacy = new Lpharmacy();
        lpharmacy.setPharmacyId(pharmacyId);
        List<Lpharmacy> lpharmacies = lDrugService.selDrug(lpharmacy);
        return lpharmacies;
    }

    //查询该患者的没有取出的所有药品清单
    @ResponseBody
    @RequestMapping("selDrugs")
    public Object selDrugs(Lrecord lrecord,Integer page, Integer limit){
        lrecord.setState(0);
        PageHelper.startPage(page,limit);
        List<Lrecord> lrecords = lDrugService.selDrugs(lrecord);
        PageInfo pageInfo = new PageInfo(lrecords);
        Map<String,Object> tableDate = new HashMap<>();
        tableDate.put("code",0);
        tableDate.put("msg","");
        tableDate.put("count",pageInfo.getTotal());
        tableDate.put("data",pageInfo.getList());
        return tableDate;
    }

    //添加患者的药品清单
    @ResponseBody
    @RequestMapping("addDrug")
    public Object addDrugs(Lrecord lrecord){
        //先查询该患者是否有这个药品信息
        lrecord.setState(0);
        //查询该患者没有取出的药品的信息
        List<Lrecord> lrecords = lDrugService.selDrugs(lrecord);
        int res = 0;
        for(int i=0;i<lrecords.size();i++){
            if(lrecords.get(i).getDurgname().equals(lrecord.getDurgname())){
                //相等表明有这个药品清单
                res = 1;
                break;
            }
        }

        if(res==1){//表明有这个药品清单，进行修改该患者的药品清单的数量和总价，同时修改药品库房中的数量
            int i = lDrugService.updateLrecord(lrecord);
            if(i==1){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else {//表明该患者没有这个药品清单，直接进行添加药品清单和减少药品库房数量即可
            int i = lDrugService.addDrugs(lrecord);
            if(i==1){//表明添加成功
                return "添加成功";
            }else{
                return "添加失败";
            }
        }
    }

    //删除该患者的药品清单根据id来删除
    @RequestMapping("delDrug")
    @ResponseBody
    public Object delDrug(Lrecord lrecord){
        int i = lDrugService.delDrug(lrecord);
        if(i==1){
            return "移除成功";
        }else{
            return "移除失败";
        }
    }


    //取药的方法
    //查询用户取过的药品信息
    @RequestMapping("/selPhar")
    @ResponseBody
    public Object selPhar(Integer page, Integer limit, Lrecord lrecord){
        lrecord.setState(2);
        PageHelper.startPage(page, limit);
        List<Lrecord> listAll =lDrugService.selDrugs(lrecord);
        PageInfo pageInfo = new PageInfo(listAll);
        Map<String, Object> tableData = new HashMap<String, Object>();
        tableData.put("code", 0);
        tableData.put("msg", "");
        tableData.put("count", pageInfo.getTotal());
        tableData.put("data", pageInfo.getList());
        return tableData;
    }


    //改变患者的药品状态
    @RequestMapping("/updDrug")
    @ResponseBody
    public Object updItem(Lrecord lrecord){
        int upd = lDrugService.updItem(lrecord);
        System.out.println(upd);
        return "取药成功";
    }


}









